package com.intel.two.service.building.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intel.two.bo.building.RegisterBo;
import com.intel.two.bo.building.ResidentBo;
import com.intel.two.common.LayuiPageVo;
import com.intel.two.mapper.buildingMapper.RegisterMapper;
import com.intel.two.pojo.building.Equipment;
import com.intel.two.pojo.building.Register;
import com.intel.two.pojo.building.Resident;
import com.intel.two.service.building.RegisterService;
import com.intel.two.service.building.ResidentService;
import com.intel.two.vo.building.BarVo;
import com.intel.two.vo.building.RegisterBarVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("registerService")
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    private RegisterMapper registerMapper;
    @Override
    public LayuiPageVo<Register> registerPageData(Map<String, Object> params) {
        //mybatis 分页 插件的使用
        Integer page = Integer.parseInt(params.get("page")+"");
        Integer limit = Integer.parseInt(params.get("limit")+"");

        PageHelper.startPage(page,limit);
        List<Register> registerList = registerMapper.registerPageData(params);
        System.out.println(registerList);

        PageInfo<Register> registerPageInfo = new PageInfo<>(registerList);

        LayuiPageVo<Register> registerLayuiPageVo = new LayuiPageVo<>();
        registerLayuiPageVo.setCode(0);
        registerLayuiPageVo.setMsg("来访人员列表");
        registerLayuiPageVo.setCount(registerPageInfo.getTotal());
        registerLayuiPageVo.setData(registerPageInfo.getList());
        return registerLayuiPageVo;
    }

    @Override
    public int addRegister(Register register) {
        return registerMapper.addRegister(register);
    }

    @Override
    public int deleteRegister(Integer id) {
        return registerMapper.deleteRegister(id);
    }

    @Override
    public int updateRegister(Register register) {
        return registerMapper.updateRegister(register);
    }

    @Override
    public List<RegisterBarVo> BarData() {
        return registerMapper.BarData();
    }

    @Override
    public List<RegisterBo> RegisterExcelData(Register register) {
        List<Register> registerExcelData = registerMapper.RegisterExcelData(register);
        List<RegisterBo> collect = registerExcelData.stream()
                .map((registers) -> {
                    RegisterBo registerBo = new RegisterBo();
                    BeanUtils.copyProperties(registers, registerBo);
                    return registerBo;
                }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public int deleteRegisters(Map<String, Object> map) {
        String str = (String) map.get("id");
        String[] strs = str.split("-");
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < strs.length; i++) {
            list.add(Integer.parseInt(strs[i]));
            System.out.println(strs[i]);
        }
        return registerMapper.deleteRegisters(list);
    }
}
